﻿// -----------------------------------------------------------------------
// <copyright file="MainLogic.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Metal.Player.Main
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using Core.Player;
    using Metal.Player.Equalizer;
    using Metal.Player.Playlist;

    /// <summary>
    /// Contains the methods for Main form. To bind this methods on your buttons, specify in your button/menu (in xml theme file): "command="MethodName"
    /// </summary>
    public class MainLogic
    {
        #region Members

        /// <summary> Stores current instance </summary>
        private static MainLogic instance = null;

        #endregion

        #region Constructor

        /// <summary> Gets the current instance of this class (Singleton - Thread safety) </summary>
        public static MainLogic Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new MainLogic();
                }

                return instance;
            }
        }

        #endregion

        #region Methods called by reflection

        /// <summary>
        /// Open file or resume if is playing
        /// </summary>
        public void Play()
        {
            try
            {
                if (Status.PlaybackStatus == Status.StatusEnum.Paused)
                {
                    Playback.Instance.Resume();
                }
                else
                {
                    // Open file dialog
                    this.OpenFile();
                }
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        /// <summary>
        /// Pause current playback
        /// </summary>
        public void Pause()
        {
            try
            {
                Playback.Instance.Pause();
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        /// <summary>
        /// Stop current playback
        /// </summary>
        public void Stop()
        {
            try
            {
                if (Status.PlaybackStatus == Status.StatusEnum.Playing || Status.PlaybackStatus == Status.StatusEnum.Paused)
                {
                    Playback.Instance.Stop();
                }
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        /// <summary>
        /// Open file dialog
        /// </summary>
        public void OpenFile()
        {
            try
            {
                OpenFileDialog fileBrowser = new OpenFileDialog();
                fileBrowser.ShowDialog();
                if (fileBrowser.FileName != string.Empty)
                {
                    Playback.Instance.Open(fileBrowser.FileName);
                    Playback.Instance.Play();
                }
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        /// <summary>
        /// Show play URL form
        /// </summary>
        public void PlayUrl()
        {
            try
            {
                Metal.CommonForms.PlayUrlForm playUrlForm = new CommonForms.PlayUrlForm();
                playUrlForm.ShowDialog();
                string url = playUrlForm.URL;
                if (url != null)
                {
                    Playback.Instance.Open(url);
                    Playback.Instance.Play();
                }

                playUrlForm.Dispose();
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        /// <summary>
        /// Show file information
        /// </summary>
        public void FileInfo()
        {
            try
            {
                Metal.CommonForms.FileInfoForm fileInfoForm = new CommonForms.FileInfoForm();
                fileInfoForm.ShowDialog();
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        /// <summary>
        /// Show general settings window
        /// </summary>
        public void GeneralSettings()
        {
            try
            {
                Metal.Config.SettingsForm settings = new Config.SettingsForm(Config.SettingsForm.ShowTab.GeneralSettings);
                settings.ShowDialog();
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        /// <summary>
        /// Show audio settings
        /// </summary>
        public void AudioSettings()
        {
            try
            {
                Metal.Config.SettingsForm settings = new Config.SettingsForm(Config.SettingsForm.ShowTab.AudioSettings);
                settings.ShowDialog();
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        /// <summary>
        /// Show video settings
        /// </summary>
        public void VideoSettings()
        {
            try
            {
                Metal.Config.SettingsForm settings = new Config.SettingsForm(Config.SettingsForm.ShowTab.VideoSettings);
                settings.ShowDialog();
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        /// <summary>
        /// Show settings form > Change theme tab
        /// </summary>
        public void Theme()
        {
            try
            {
                Metal.Config.SettingsForm settings = new Config.SettingsForm(Config.SettingsForm.ShowTab.Themes);
                settings.ShowDialog();
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        /// <summary>
        /// Show equalizer window
        /// </summary>
        public void ViewEqualizer()
        {
            try
            {
                if (EqualizerController.View.IForm != null)
                {
                    if (EqualizerController.View.IForm.Visible == true)
                    {
                        EqualizerController.View.IForm.Visible = false;
                    }
                    else
                    {
                        EqualizerController.View.IForm.Visible = true;
                    }
                }
                else
                {
                    EqualizerController.View.IForm.Show();
                }
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }
        
        /// <summary>
        /// Show media library window
        /// </summary>
        public void ViewMediaLibrary()
        {
            try
            {
                if (PlaylistController.CurrentView.IForm != null)
                {
                    if (PlaylistController.CurrentView.IForm.Visible == true)
                    {
                        PlaylistController.CurrentView.IForm.Visible = false;
                    }
                    else
                    {
                        PlaylistController.CurrentView.IForm.Visible = true;
                    }
                }
                else
                {
                    PlaylistController.CurrentView.IForm.Show();
                }
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        #endregion
    }
}
